from common.connect_db import MyDB

my_db = MyDB()


class DataBaseOnOrder:

    @staticmethod
    def assert_life_online_orders(**kwargs):
        """
        断言订单表，根须自己所需条件传入参数
        :param kwargs:
        :return:
        """
        condition = ""
        for k, v in kwargs.items():
            condition += ' and ' + str(k) + "='" + str(v) + "'"
        sql = 'select * from life_online_orders where' + condition[4:]
        result = my_db.execute_select('onorder', sql)
        if len(result) == 1:
            return True, result[0].id
        else:
            return False

    @staticmethod
    def assert_life_online_order_goods(**kwargs):
        """
        :param kwargs:
        :return:
        """
        condition = ""
        for k, v in kwargs.items():
            condition += ' and ' + str(k) + "='" + str(v) + "'"
        sql = 'select * from life_online_order_goods where' + condition[4:]
        result = my_db.execute_select('onorder', sql)
        if len(result) == 1:
            return True
        else:
            return False

    @staticmethod
    def get_life_online_orders_list(**kwargs):
        """
        根须自己所需条件传入参数
        :param kwargs:
        :return:
        """
        condition = ""
        for k, v in kwargs.items():
            condition += ' and ' + str(k) + "='" + str(v) + "'"
        sql = 'select * from life_online_orders where' + condition[4:]
        result = my_db.execute_select('onorder', sql)
        if len(result) >= 1:
            return result
        else:
            raise ValueError("没有查询到数据！")

    def get_actual_amount(ordersn):
        """
        根据订单号获取实际付款金额
        :param ordersn:
        :return:
         """
        sql = "select actual_amount from life_online_orders " " " \
              "where ordersn ='%s'" % ordersn
        actual_amount = my_db.execute_select("onorder", sql)
        if actual_amount[0]:
            return actual_amount[0][0]
        else:
            raise ValueError("Data Error!")


if __name__ == '__main__':
    test = DataBaseOnOrder()
    test.assert_life_online_orders(
        key="1",
        ky=2
    )
